package org.jeecgframework.tag.core.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件工具类,只是开启时读取一次,没有必要当作工具类
 * @author JueYue
 * @date 2014年12月3日 上午12:28:44
 */
public class FileUtil {

    private Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 获取文件text
     * @param path
     * @return
     */
    public String getFileConten(String path) {
        InputStream is = null;
        BufferedReader bufferedReader = null;
        try {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
            bufferedReader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            StringBuilder sbr = new StringBuilder();
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                sbr.append(lineTxt);
            }
            return sbr.toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e.fillInStackTrace());
            throw new RuntimeException(e);
        } finally {
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
                if (is != null)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
